<template>
  <div>
    <div>【关键词条】</div>
    <div ref="target" class="w-full h-full"></div>
  </div>
</template>
<script setup>
import { ref, onMounted, watch } from "vue";
import * as echarts from "echarts";
import "echarts-wordcloud";
const props = defineProps({
  data: {
    type: Object,
    required: true,
  },
});
console.log(props.data);

// 1. 初始化echarts实例
let myChart = null;
const target = ref(null);
onMounted(() => {
  myChart = echarts.init(target.value);
  renderChart();
});

const randomRGB = () => {
  const r = Math.floor(Math.random() * 255);
  const g = Math.floor(Math.random() * 255);
  const b = Math.floor(Math.random() * 255);
  return `rgb(${r},${g},${b})`;
};

// 2. 构建options配置对象
const renderChart = () => {
  const options = {
    series: [
      {
        type: "wordCloud",
        sizeRange: [8, 46], // 字体大小范围
        rotationRange: [0, 0], // 旋转角度
        gridSize: 0, // 每个词之间的间距
        layoutAnimation: true, // 是否启用动画
        textStyle: {
          color: randomRGB,
        },
        emphasis: {
          // 鼠标悬停时的样式
          textStyle: {
            fontWeight: "bold",
            color: "#000",
          },
        },
        data: props.data.datas,
      },
    ],
  };
  myChart.setOption(options);
};

watch(() => props.data, renderChart);
// 3. 通过实例.setOption(option)渲染图表
</script>
<style lang="scss" scoped>
</style> 